/*
 * Copyright (c) gitee.com/jiandong001.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import ArrayList from '@ohos.util.ArrayList';
import hilog from '@ohos.hilog';

import { DevSupportManagerFactory } from './devSupport/DevSupportManagerFactory';
//import { DVModelContainer } from './uimanager/DynamicView';
import { ReactPackage } from './ReactPackage';

import { ReactInstanceManagerBuilder } from './ReactInstanceManagerBuilder'
import { UIImplementationProvider } from './uimanager/UIImplementationProvider'
import { JavaScriptExecutorFactory } from './bridge/JavaScriptExecutorFactory'
import { ReactInstanceManager } from './ReactInstanceManager'
import { JSIModulePackage } from './bridge/JSIModulePackage'
import { SurfaceDelegate } from './common/SurfaceDelegate'
import { SurfaceDelegateFactory } from './common/SurfaceDelegateFactory'

//TOOD : should not use single instance
export class ReactNativeHost {
  //    private readonly  mApplication;
  private static readonly TAG: string = "ReactNativeHost"
  private static mInstance: ReactNativeHost = new ReactNativeHost()
  private mReactInstanceManager: ReactInstanceManager
  private builder: ReactInstanceManagerBuilder

  private constructor() {
    hilog.info(0x0000, ReactNativeHost.TAG, '%{public}s', 'constructor');
  }

  public static getInstance(): ReactNativeHost {
    //      if (ReactNativeHost.mInstance == null) {
    //        hilog.info(0x0000, ReactNativeHost.TAG, '%{public}s', 'getInstance is null');
    //        ReactNativeHost.mInstance = new ReactNativeHost();
    //      }
    return ReactNativeHost.mInstance;
  }

  public static getJavaScriptExecutorFactory(): JavaScriptExecutorFactory | null {
    return null;
  }

  public static getUIImplementationProvider(): UIImplementationProvider {
    return new UIImplementationProvider();
  }

  //    public clear() {
  ////        this.mReactInstanceManager?.destroy();
  //        this.mReactInstanceManager = null;
  //    }

  //
  public static getJSIModulePackage(): JSIModulePackage | null {
    return null;
  }

  //    private static  createReactInstanceManager(): ReactInstanceManager {
  //        return ReactNativeHost.getReactInstanceManagerBuilder().build();
  //    }

  //
  public static getSurfaceDelegateFactory(): SurfaceDelegateFactory | null {
    return (() => {
      return new class temp extends SurfaceDelegateFactory {
        public createSurfaceDelegate(var1: string): SurfaceDelegate | null {
          return null;
        }
      }();
    })();
  }

  //    private static  getReactPackageTurboModuleManagerDelegateBuilder(): Builder | null {
  //        return null;
  //    }

  public static getJSMainModuleName(): string {
    return "index.harmony";
  }

  public static getJSBundleFile(): string | null {
    return null;
  }

  public static getBundleResourceName(): string | null {
    //todo: should chanage to index.harmony.bundle
    return "index.android.bundle";
  }

  public getReactInstanceManager(): ReactInstanceManager {
    //        return ReactNativeHost.mReactInstanceManager??= ReactNativeHost.createReactInstanceManager()
    return this.mReactInstanceManager
  }

  public setReactInstanceManager(instanceManager: ReactInstanceManager): void {
    hilog.info(0x0000, ReactNativeHost.TAG, '%{public}s', 'setReactInstanceManager');
    this.mReactInstanceManager = instanceManager
  }

  public getReactInstanceManagerBuilder(): ReactInstanceManagerBuilder {
    if (!this.builder) {
      this.builder = new ReactInstanceManagerBuilder()
    }
    return this.builder
    //return ReactNativeHost.builder ??= new ReactInstanceManagerBuilder()
  }

  // abstract
  public getUseDeveloperSupport(): boolean {
    return true;
  }

  protected getDevSupportManagerFactory(): DevSupportManagerFactory | null {
    return null;
  }

  // protected abstract
  protected getPackages(): ArrayList<ReactPackage> {
    return null;
  }
}
